<!--
  - Copyright (c) 2021.  北京维数科技有限公司
  - Date :  2021/3/25
  - Author ：F12 Console=> atob("MzMxNDkxODAwQHFxLmNvbQ==")
  - Version ：1.0.0
  -->

<template>
  <div class="app-container calendar-list-container no-padding">
    <el-radio-group v-model="warningType" @change="changeType" style="margin-bottom: 30px;margin-top: 15px">
      <el-radio-button label="1">报警通知</el-radio-button>
      <el-radio-button label="2">故障通知</el-radio-button>
    </el-radio-group>
    <div class="warning-notice-wrap">
      <div class="warning-notice-title">
        <span class="icon-blue-dot"></span>
        通知方式
      </div>
      <el-row class="warning-radio-box">
        <el-checkbox-group v-model="noticeIds" :min=1 @change="warningTypeChange">
          <el-checkbox v-for="item in warningTypeList" :label="item.id" :key="item.id">{{item.notice}}</el-checkbox>
        </el-checkbox-group>
      </el-row>
      <div class="warning-notice-title warning-notice_mgbt">
        <span class="icon-blue-dot"></span>
        消息接收人
      </div>
      <el-table :key='tableKey' :data="list" v-loading.body="listLoading" style="width: 100%">
        <el-table-column label="用户名">
          <template slot-scope="scope">
            <span>{{scope.row.username}}</span>
          </template>
        </el-table-column>
        <el-table-column label="真实姓名">
          <template slot-scope="scope">
            <span>{{scope.row.name}}</span>
          </template>
        </el-table-column>
        <el-table-column label="手机号">
          <template slot-scope="scope">
            <span>{{scope.row.mobilePhone}}</span>
          </template>
        </el-table-column>
        <el-table-column label="角色">
          <template slot-scope="scope">
            <span>{{scope.row.groupName}}</span>
          </template>
        </el-table-column>
        <el-table-column label="推送规则">
          <template slot-scope="scope">
            <span v-if="scope.row.hasNoticeRule===0">未配置</span>
            <span v-else>已配置</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <el-button size="small" type="text" @click="handleStop(scope.row)">
              推送规则
            </el-button>
            <el-button size="small" type="text" @click="handleDelete(scope.row)">
              刪除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div v-show="!listLoading" class="pagination-container">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="listQuery.page" :page-sizes="[10,15,30, 50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
      </div>
      <el-row>
        <el-button type="text" @click="Show=true">+添加消息接收人</el-button>
      </el-row>
    </div>
    <el-dialog title="删除消息接收人" :visible.sync="dialogDeleteVisible" :show-close='false' custom-class="xf-jc-dialog" width="360px">
      <div class="xf-jc-deletect">
        <p>是否删除此消息接收人</p>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="danger" size="mini"  @click="deleteObj()">删除</el-button>
        <el-button type="text" size="mini"  @click="dialogDeleteVisible = false">取消</el-button>
      </div>
    </el-dialog>
    <ReceivePeople :Show.sync="Show" :warningType.sync="warningType" @sureReceive="sureReceive"></ReceivePeople>
    <noticRule :Show.sync="nShow" :noticRuleObj.sync="noticRuleobj" @sureReceive="sureReceive"></noticRule>
  </div>
</template>

<script>

import {
  getNoticeType,
  changeNoticeType,
  page,
  delObj,
} from "@/api/device/deviceMessageNotice/index";
import ReceivePeople from "@/components/ReceivePeople";
import noticRule from "@/components/noticRulelist";
export default {
  name: "warningNotice",
  components:{ReceivePeople,noticRule},
  data() {
    return {
      warningType:'1',
      warningTypeList:[],
      noticeIds:[],//通知的方式
      list: null,
      total: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 15,
      },
      dialogFormVisible: false,
      dialogDeleteVisible:false,
      currentSelectObj:{},
      dialogStatus: "",
      tableKey: 0,
      Show:false,
      nShow:false,
      noticRuleobj:{}
    };
  },
  created() {
    this.ntypeQuery();
    this.getList();
  },
  methods: {
    changeType(){
      this.tableKey===0?this.tableKey=1:this.tableKey=0;
    //  tab触发点击
      this.ntypeQuery();
      this.getList();
    },
    ntypeQuery(){
      //通知方式查询
      getNoticeType({'noticeType':this.warningType}).then(res=>{
         this.warningTypeList = res.data.notice;
         let notictTypeList = this.warningTypeList;
         let tarr = [];
         notictTypeList.forEach((item)=>{
            if (item.checked===true){
              tarr.push(item.id)
            }
         });
         this.noticeIds = tarr;
      })
    },
    handleStop(row){
      //推送规则操作
      this.nShow = true;
      this.noticRuleobj = Object.assign({},row,{'noticeType':this.warningType});
    },
    warningTypeChange(val){
    //  改变通知方式
      changeNoticeType({'noticeType':this.warningType,ids:val.join(',')}).then(res=>{
         if (res.status===200){
           this.$message({
             message: '修改成功',
             type: 'success'
           });
         }
      })
    },
    getList() {
      this.listLoading = true;
      page(Object.assign(this.listQuery,{'noticeType':this.warningType})).then(response => {
        this.list = response.data.rows;

        this.total = response.data.total;
        this.listLoading = false;
      });
    },

    handleSizeChange(val) {
      this.listQuery.limit = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.page = val;
      this.getList();
    },
    deleteObj(){
      delObj({'noticeType':this.warningType,id:this.currentSelectObj.id}).then(res=>{
        if (res.status===200){
          this.$notify({
            title: "成功",
            message: "删除成功",
            type: "success",
            duration: 2000
          });
          this.dialogDeleteVisible = false;
          this.getList();
        }
      })
    },
    handleDelete(row){
      this.currentSelectObj = row;
      this.dialogDeleteVisible = true;
    },
    sureReceive(){
      this.Show=false;
      this.getList();
    }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss">
  .warning-notice-wrap{
    .warning-radio-box{
      padding-top: 10px;
    }
    .warning-notice-title{
      font-size: 16px;
      color: rgba(255,255,255,.7);
      font-family:PingFangSC-Medium;
      line-height: 22px;
      margin-bottom: 10px;
      .icon-blue-dot{
        display: inline-block;
        width: 10px;
        height: 10px;
        background-color: #50E3C2;
        border-radius: 50%;
      }
    }
    .warning-notice_mgbt{
      margin-top: 40px;
    }
  }
.el-table th {
  background: #f5f7fa;
}
.xf-query-input,
.xf-query-select {
  width: 200px;
  margin: 0 10px;
}
.warning-notice-fulldialog > div {
  width: 800px;
  margin: 0 auto;
}
.warning-notice-fulldialog .el-dialog__title {
  font-size: 32px;
}
.warning-notice-fulldialog {
  .el-dialog__header {
    margin-top: 20px;
  }
  .custom-dialog-btn {
    position: absolute;
    font-size: 30px;
    left: 40px;
    top: 20px;
    color: #fff;
    &:hover {
      cursor: pointer;
    }
  }
  .dialog-footer {
    text-align: left;
  }
  .el-color-picker--medium .el-color-picker__mask{
  height: 32px;
  }
  .el-checkbox{
    margin-right:30px;
    margin-left:0;
  }
}
.el-input {
  position: relative;
}
.receive-pagination-container{
  display:flex;
  justify-content:space-between;
}
</style>

